package com.hema.auction.utils.manager;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * author by john
 * 本地存储工具类
 */
public class SPManager {
    private static final String SP_NAME = "auction";
    private static SPManager sInstance;
    private Context mContext;
    private SharedPreferences sp;
    private SharedPreferences.Editor mEditor;

    private SPManager(Context context) {
        this.mContext = context;
    }

    public static SPManager getInstance(Context context) {
        if (null == sInstance) {
            sInstance = new SPManager(context);
        }
        return sInstance;
    }

    private SharedPreferences getSharedPreferences() {
        if (null == sp) {
            sp = mContext.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        }
        return sp;
    }

    private SharedPreferences.Editor getEditor() {
        if (null == mEditor) {
            mEditor = getSharedPreferences().edit();
        }
        return mEditor;
    }

    public boolean clearInfo() {
        SharedPreferences.Editor editor = getEditor();
        editor.remove(IS_INIT);
        editor.remove(USER_NAME);
        editor.remove(PASSWORD);
        editor.remove(IS_LOGIN);
        editor.remove(NEW_MESSAGE_COUNT);
        editor.remove(ID);
        editor.remove(USER_ID);
        return editor.commit();
    }

    private static final String USER_NAME = "username";
    private static final String IS_INIT = "is_init";
    private static final String PASSWORD = "password";
    private static final String TOKEN = "token";
    private static final String USER_ID = "user_id";
    private static final String IS_LOGIN = "islogin";
    private static final String ID = "id";
    private static final String NEW_MESSAGE_COUNT = "new_message_count";
    private static final String HTTP_VERSION_CODE = "http_version_code";
    private static final String HTTP_APK_URL = "http_apk_url";
    private static final String SPLASH_IMG = "splash_img";
    private static final String SHOW_GUIDE = "show_guide";
    private static final String SUSPENSION_AD = "suspension_ad";
    private static final String SHOW_NO_BAOPEI = "show_no_baopei";

    public String getUserId() {
        return getSharedPreferences().getString(USER_ID, "");
    }

    public boolean setUserId(String userId) {
        return getEditor().putString(USER_ID, userId).commit();
    }

    public String getUserName() {
        return getSharedPreferences().getString(USER_NAME, "");
    }

    public boolean setUserName(String username) {
        return getEditor().putString(USER_NAME, username).commit();
    }

    public String getPassword() {
        return getSharedPreferences().getString(PASSWORD, "");
    }

    public boolean setPassword(String password) {
        return getEditor().putString(PASSWORD, password).commit();
    }


    public boolean islogin() {
        return getSharedPreferences().getBoolean(IS_LOGIN, false);
    }

    public boolean setIsLogin(boolean isLogin) {
        return getEditor().putBoolean(IS_LOGIN, isLogin).commit();
    }

    public String getToken() {
        return getSharedPreferences().getString(TOKEN, "");
    }

    public boolean setToken(String token) {
        return getEditor().putString(TOKEN, token).commit();
    }

    public int getId() {
        return getSharedPreferences().getInt(ID, -1);
    }

    public boolean setId(int id) {
        return getEditor().putInt(ID, id).commit();
    }

    public int getNewMessageCount() {
        return getSharedPreferences().getInt(NEW_MESSAGE_COUNT, 0);
    }

    public boolean setNewMessageCount(int count) {
        return getEditor().putInt(NEW_MESSAGE_COUNT, count).commit();
    }

    public int getHttpVersionCode() {
        return getSharedPreferences().getInt(HTTP_VERSION_CODE, 0);
    }

    public boolean setHttpVersionCode(int code) {
        return getEditor().putInt(HTTP_VERSION_CODE, code).commit();
    }

    public String getHttpApkUrl() {
        return getSharedPreferences().getString(HTTP_APK_URL, "");
    }

    public boolean setHttpApkUrl(String url) {
        return getEditor().putString(HTTP_APK_URL, url).commit();
    }

    public String getSplashImg() {
        return getSharedPreferences().getString(SPLASH_IMG, "");
    }

    public boolean setSplashImg(String url) {
        return getEditor().putString(SPLASH_IMG, url).commit();
    }

    public boolean hasShowGuide() {
        return getSharedPreferences().getBoolean(SHOW_GUIDE, false);
    }

    public boolean setShowGuide(boolean hasShow) {
        return getEditor().putBoolean(SHOW_GUIDE, hasShow).commit();
    }

    public String getSuspensionAd() {
        return getSharedPreferences().getString(SUSPENSION_AD, "");
    }

    public boolean setSuspensionAd(String ad) {
        return getEditor().putString(SUSPENSION_AD, ad).commit();
    }

    public boolean needShowNoBaopei() {
        return getSharedPreferences().getBoolean(SHOW_NO_BAOPEI, false);
    }

    public boolean setShowNoBaopei(boolean needShow) {
        return getEditor().putBoolean(SHOW_NO_BAOPEI, needShow).commit();
    }

    public boolean isInit() {
        return getSharedPreferences().getBoolean(IS_INIT, true);
    }

    public boolean setInit(boolean isiNit) {
        return getEditor().putBoolean(IS_INIT, isiNit).commit();
    }

}
